package com.example.dllo.my36kr.news.project;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.tools.NetTool;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

/**
 * Created by dllo on 16/8/20.
 */
public class ProjectAdapter extends BaseAdapter {
    Context mContext;
    ProjectBean list;
    private final NetTool mNetTool;
    SimpleDateFormat mformat;

    public void setList(ProjectBean list) {
        this.list = list;
    }

    public ProjectAdapter(Context mContext) {
        this.mContext = mContext;
        mNetTool = new NetTool();
        mformat = new SimpleDateFormat("HH:mm");
    }

    @Override
    public int getCount() {
        return list != null ? list.getData().getData().size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ProjectViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_project,null);
            holder = new ProjectViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ProjectViewHolder) convertView.getTag();
        }
        holder.titleTv.setText(list.getData().getData().get(position).getTitle());
        holder.nameTv.setText(list.getData().getData().get(position).getUser().getName());
        mNetTool.getImage(list.getData().getData().get(position).getFeatureImg(),holder.featureImgIv);
        Long now = System.currentTimeMillis();
        long dTime = now - Long.valueOf(list.getData().getData().get(position).getPublishTime());
        SimpleDateFormat nFormat = new SimpleDateFormat("dd");
        int nNow = Integer.parseInt(nFormat.format(now)) - Integer.parseInt(nFormat.format(Long.valueOf(list.getData().getData().get(position).getPublishTime())));
        if (nNow >= 1) {
            holder.publishTimeTv.setText(nNow + "天之前");
        }else if (dTime > 1000 * 60 * 60){
            //显示正常日期
            String time = mformat.format(Long.valueOf(dTime));
            holder.publishTimeTv.setText(time);

        } else if (dTime > 1000 * 60) {
            //显示多长分钟之前
            String min = String.valueOf((int) dTime / 1000 / 60);
            holder.publishTimeTv.setText(min + "分钟之前");
        } else {
            //显示多少s之前
            String second = String.valueOf((int) dTime / 1000);
            holder.publishTimeTv.setText(second + "s之前");
        }

        return convertView;
    }
    public class ProjectViewHolder{
        ImageView featureImgIv;
        TextView titleTv,publishTimeTv,nameTv;
        public ProjectViewHolder(View view) {
            featureImgIv = (ImageView) view.findViewById(R.id.iv_news_project_featureImg);
            titleTv = (TextView) view.findViewById(R.id.tv_news_project_title);
            publishTimeTv = (TextView) view.findViewById(R.id.tv_news_project_publishTime);
            nameTv = (TextView) view.findViewById(R.id.tv_news_project_name);
        }
    }
}
